<template>
  <div class="app-container">
    <div class="app-tools">
      <el-button type="info" icon="el-icon-plus" size="mini" @click="dialogVisible = true">新增</el-button>
    </div>
    <div class="app-body">
      <el-table
        border
        stripe
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="fdId"
          label="编号"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="fdName"
          label="名称"
          width="240"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="fdKey"
          label="流程编号"
          width="240"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="fdCreateTime"
          label="开始时间"
          width="240"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="fdUpdateTime"
          label="结束时间"
          width="240"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="流程状态"
          width="240"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.fdDeployed" size="mini">成功</el-tag>
            <el-tag type="error" v-if="!scope.row.fdDeployed" size="mini">失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
            <el-button type="text" size="small" @click="handleForm(scope.row)">编辑表单</el-button>
            <el-button type="text" size="small" @click="handleFlow(scope.row)">编辑流程</el-button>
            <el-button type="text" size="small" @click="handleDeploy(scope.row)">发布流程</el-button>
            <el-button type="text" size="small" @click="handleProcess(scope.row)">发起实例</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="20%">
      <el-form :model="ruleForm" ref="ruleForm" label-width="100px">
        <el-form-item label="模板名称" prop="fdName">
          <el-input v-model="ruleForm.fdName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import {getList, save, deleteOne, deploy, process} from "../../api";

export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      ruleForm: {
        fdName: '',
      }
    }
  },
  methods: {
    handleQuery() {
      getList().then(res => {
        if (res && res.data.code === 20000) {
          this.tableData = res.data.data;
        }
      });
    },
    handleForm(row) {
      this.$router.replace('/form?fdId=' + row.fdId)
    },
    handleFlow(row) {
      this.$router.replace('/flowy?fdId=' + row.fdId)
    },
    handleDeploy(row) {
      deploy(row.fdId).then(res => {
        if (res && res.data.code === 20000) {
          this.$message({
            message: '部署成功',
            type: 'success'
          });
          this.handleQuery();
        }
      });
    },
    handleProcess(row) {
      process(row.fdId).then(res => {
        if (res && res.status === 200) {
          this.$message({
            message: '流程实例发起成功',
            type: 'success'
          });
          this.handleQuery();
        }
      });
    },
    handleDelete(row) {
      deleteOne(row.fdId).then(res => {
        if (res && res.data.code === 20000) {
          this.$message({
            message: '保存成功',
            type: 'success'
          });
          this.handleQuery();
        }
      });
    },
    onSubmit() {
      save(this.ruleForm).then(res => {
        if (res && res.data.code === 20000) {
          this.$message({
            message: '保存成功',
            type: 'success'
          });
          this.dialogVisible = false;
          this.handleQuery();
        }
      });
    }
  },
  mounted() {
    this.handleQuery();
  }
}
</script>

<style scoped>
.app-container {
  display: flex;
  height: 100%;
  width: 96%;
  flex-direction: column;
}

.app-tools {
  display: flex;
  justify-content: start;
  padding-top: 20px;
  padding-bottom: 20px;
}
</style>
